package com.quasar.hpatient.view.adapter;

import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.litePalmodel.HospitalInfodb;
import com.quasar.hpatient.model.litePalmodel.PositonInfodb;
import com.quasar.hpatient.model.medicalmodel.DoctorListBean;
import com.quasar.hpatient.network.RetrofitConnection;
import com.quasar.hpatient.utils.Spanny;
import com.vise.log.ViseLog;

import java.util.List;

/**
 * author : 许建宏
 * Date: 2017/10/12 0012
 * View:我的医生
 */

public class DoctorAdapter extends BaseQuickAdapter<DoctorListBean, BaseViewHolder> {

    public DoctorAdapter() {
        super(R.layout.doctor_item_follow);
    }

    //医院
    List<HospitalInfodb> hospitalInfodbList = MainAppliaction.getInstance().getHospitalInfodbList();
    //科室
    List<PositonInfodb> positonInfodbList = MainAppliaction.getInstance().getPositonInfoList();

    @Override
    protected void convert(BaseViewHolder helper, DoctorListBean item) {
        String message = null;

        TextView tv_index = helper.getView(R.id.tv_index);
        if (helper.getLayoutPosition() == 0 || !DoctorAdapter.this.getData().get(helper.getLayoutPosition() - 1).getIndex().equals(item.getIndex())) {
            tv_index.setVisibility(View.VISIBLE);
            tv_index.setText(item.getIndex());
        } else {
            tv_index.setVisibility(View.GONE);
        }

        //如果昵称不存在，显示用户名
        String userName = "";
        if (item.getRealName() != null && item.getRealName().length() > 0) {
            helper.setText(R.id.name, item.getRealName());//真实姓名
            userName = item.getRealName();
        } else {
            userName = "匿名";
        }
        helper.setText(R.id.name, userName);//真实姓名

        //最近消息动态
        if (item.getDoctorDynamicInfo() != null) {
            if (item.getDoctorDynamicInfo().getDynamics().length() > 0) {
                message = item.getDoctorDynamicInfo().getDynamics();
            }
        } else {
            message = "无最新动态";
        }
        Spanny spanny = new Spanny();
        spanny.append("动态：", new ForegroundColorSpan(color(R.color.btcolor)));
        spanny.append("" + message, new ForegroundColorSpan(color(R.color.textcolor)));
        helper.setText(R.id.tv_information, spanny);

        String HospitalName = "";
        //医院
        if (item.getHospitalId() != 0) {
            int size = hospitalInfodbList.size();
            for (int i = 0; i < size; i++) {
                if (item.getHospitalId() == hospitalInfodbList.get(i).getPrimaryid()) {
                    HospitalName = hospitalInfodbList.get(i).getName();
                    break;
                }
            }
        } else {
            HospitalName = "未知";
        }
        helper.setText(R.id.tv_hospital, HospitalName);

        //职称
        String positionName = "";
        if (item.getTitleId() != 0) {
            for (int i = 0; i < positonInfodbList.size(); i++) {
                if (item.getTitleId() == positonInfodbList.get(i).getPrimaryid()) {
                    positionName = positonInfodbList.get(i).getName();
                    break;
                }
            }
        } else {
            positionName = "未知";
        }
        helper.setText(R.id.tv_job_title, positionName);

        //头像
        if (item.getCover() != null && item.getCover().length() > 0) {
            Glide.with(mContext).load(RetrofitConnection.PHOTOURL + item.getCover()).into((ImageView) helper.getView(R.id.headimg));
        } else {
            Glide.with(mContext).load(R.mipmap.doctor).into((ImageView) helper.getView(R.id.headimg));
        }

        //交流按钮
        helper.addOnClickListener(R.id.tv_communication);
        //头像
        helper.addOnClickListener(R.id.headimg);


        //我的医生列表显示待诊断信息
        RelativeLayout relativeLayout = helper.getView(R.id.rl_examine);
        if (item.getDoctorPatientRelative() != null) {
            //判断医生状态
            ViseLog.d("医生的状态" + item.getDoctorPatientRelative().getRelationType());
            if (item.getDoctorPatientRelative().getRelationType() == ConstantManger.Relation1) {
                relativeLayout.setVisibility(View.VISIBLE);
            } else if (item.getDoctorPatientRelative().getRelationType() == ConstantManger.Relation2
                    || item.getDoctorPatientRelative().getRelationType() == ConstantManger.Relation4
                    || item.getDoctorPatientRelative().getRelationType() == ConstantManger.stopRelation) {
                relativeLayout.setVisibility(View.GONE);
            } else {
                relativeLayout.setVisibility(View.GONE);
            }
        } else {
            relativeLayout.setVisibility(View.GONE);
        }
    }

    private int color(int value) {
        return MainAppliaction.getInstance().getResources().getColor(value);
    }
}
